<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\controller\BaseController;
use cmf\controller\HomeBaseController;
use think\Db;
use think\Request;
use think\Cookie;
use think\Session;
class IndexController extends HomeBaseController
{
    public function index(Request $request)
    {
       // dump(Session::get('uid'));
        Cookie::clear('qinqin');
        $this->assign('hot',input('id'));
        if($request->isAjax()){
            $page =  $request->param('page');
           if(Cookie::get('pages'.$page,'qinqin'))
           {
              //return json('11111111111111');
               $teachers = Cookie::get('pages'.$page,'qinqin');
           }
            else{
               //return json('22222222222222222');
                $teachers=Db::name("teacher")->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->where('status',1)
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.picture,qq_teacher.education,qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->page($page,5)->select()->toArray();
                Cookie::set('pages'.$page,$teachers);
            }
            return json($teachers);
        }
        else{
            if(!input('city')){
                $this->assign('city','');
            }
            else{
                $this->assign('city',input('city'));
            }
           // dump(Cookie::get());
            /* dump(Cookie::get('teacher','qinqin'));*/
            if(Cookie::get('teacher','qinqin')){
                //dump('1');
                $teachers['data']=Cookie::get('teacher','qinqin');
            }
            else{
                //dump('2');
                $teachers=Db::name("teacher")->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->where('status',1)
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.picture,qq_teacher.education,qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->paginate(5)
                    ->toArray();
                /* $teacher=Db::name("teacher")->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->where('status',1)
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.picture,qq_teacher.education,qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->page(1,1)->select()->toArray();
                dump($teacher);
                Cookie::set('pages',$teacher);
                Cookie::get('pages','qinqin');
                die;*/
                //Cookie::set('teacher',$teachers['data'],['prefix'=>'qinqin','expire'=>3600]);
                //设置缓存
                Cookie::set('teacher',$teachers['data']);
              /*  Cookie::set('pages',$teacher);*/
                // dump(Cookie::get('teacher','qinqin'));
                /*dump($teachers['data']);
                dump(Cookie::get());
                dump(Cookie::get('teacher','qinqin'));
                die;*/
            }
            $this->assign('teachers',$teachers['data']);
        }

        $sliders=Db::name('slide_item')->where('slide_id',1)->limit(3)->order('id', 'asc')->select();
        $this->assign('last',$sliders[count($sliders)-1]);
        $this->assign('first',$sliders[0]);
        $this->assign("sliders",$sliders);
        return $this->fetch(':index');
    }
    public function city(){
        return $this->fetch(':city');
    }
}
